import {QL} from 'iflux2';
import moment from 'moment';
import {fromJS, List} from 'immutable';

export const stepQL = QL('stepQL', [
    'detail',
    (detail)=>{
        let steps = [];
        let ctime = detail.get('ctime');//下单时间
        let ptime = detail.get('ptime');//支付时间
        let dtime = detail.get('dtime');//发货时间
        let etime = detail.get('etime');//完结时间

        steps.push({title: '提交订单', time: !!ctime ? moment(ctime).format('YYYY/MM/DD HH:mm:ss') : ''});
        ['GRP', 'CAP'].indexOf(detail.get('payTypeId')) == -1 && steps.push({title: '买家已付款', time: !!ptime ? moment(ptime).format('YYYY/MM/DD HH:mm:ss') : ''});
        steps.push({title: '卖家已发货', time: !!dtime ? moment(dtime).format('YYYY/MM/DD HH:mm:ss') : ''});
        steps.push({title: '买家已收货', time: !!etime ? moment(etime).format('YYYY/MM/DD HH:mm:ss') : ''});

        return fromJS(steps);
    }
]);

export const currentQL = QL('currentQL', [
    'detail',
    (detail)=>{
        let current = -1;
        let orderStatus = detail.get('orderStatus');
        if (orderStatus === 'WAIT_PAY') {
            current = 0;
        } else if (orderStatus === 'WAIT_DELIVER') {
            current = 1;
        } else if (orderStatus === 'WAIT_RECEIVE') {
            current = 2;
        } else if (orderStatus === 'SUCCESS' && detail.get('isApplyReturn') == 1) {
            current = 3;
        } else if (orderStatus === 'SUCCESS' && detail.get('isApplyReturn') == 0){
            current = 4;
        } else if (orderStatus === 'CLOSE') {
            current = 5;
        } else if (orderStatus === 'UNPAY_CLOSE') {
            current = 6
        }
        return current;
    }
]);

export const tipQL = QL('tipQL', [
    'detail',
    (detail)=>{
        let orderStatus = detail.get('orderStatus');
        let tip = '待支付';
        if (orderStatus === 'WAIT_DELIVER') {
            tip = '待发货';
        } else if (orderStatus === 'WAIT_RECEIVE') {
            tip = '待收货';
        } else if (orderStatus === 'SUCCESS') {
            tip = '已完成';
        } else if (orderStatus === 'CLOSE') {
            tip = '已取消';
        } else if (orderStatus === 'UNPAY_CLOSE') {
            tip = '已取消';
        }
        return tip;
    }
]);

export const dataSourceQL = QL('dataSourceQL', [
    'detail',
    (detail)=>(detail.get('itemOrders') || List()).sortBy(item=>item.get('isGift')).map((item, index)=>item.set('key', `${item.get('skuId')}_${index}`).toJS()).toArray()
])